#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <jsoncpp/json/json.h>
#include "../../../NetEngine_Linux/NetEngine_Release/NetEngine_Include/XyRyNet_CommHdr.h"
#include "../../../NetEngine_Linux/NetEngine_Release/NetEngine_Include/XyRyNet_Types.h"
#include "../../../NetEngine_Linux/NetEngine_Release/NetEngine_Include/XyRyNet_ProtocolHdr.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/NetEngine_Client/NetClient_Socket/NetClient_Define.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/NetEngine_Client/NetClient_Socket/NetClient_Error.h"

//g++ -std=gnu++11 -Wall -g TestFor_MQService.cpp -o TestFor_MQService -L ../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_BaseLib -L ../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_Client -lNetEngine_BaseLib -lNetClient_Socket -ljsoncpp -Wl,-rpath=../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_BaseLib:../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_Client,--disable-new-dtags
SOCKET m_Socket;

void ServiceApp_Stop(int signo)
{
    printf("客户端退出！\n");
    NetClient_TCPSelect_Close(m_Socket);
    exit(0);
}

void MQ_Post(LPCTSTR lpszMsgBuffer, TCHAR *ptszMsgBuffer, int *pInt_Len)
{
    LPCTSTR lpszAddr = _T("XYRYMQ_CODE");

    NETENGINE_PROTOCOLHDR st_ProtocolHdr;
    memset(&st_ProtocolHdr, '\0', sizeof(NETENGINE_PROTOCOLHDR));

    st_ProtocolHdr.wHeader = NETENGIEN_COMMUNICATION_PACKET_PROTOCOL_HEADER;
    st_ProtocolHdr.unOperatorType = ENUM_ENGINE_COMMUNICATION_PROTOCOL_TYPE_XMQ;
    st_ProtocolHdr.unOperatorCode = NETENGINE_COMMUNICATION_PROTOCOL_OPERATOR_CODE_MQ_REQPOST;
    st_ProtocolHdr.byVersion = 1;
    st_ProtocolHdr.wPacketSerial = 0;          //序列号,如果永久存在包，这个值需要设置，你自己管理，不然你无法获取多个包
    st_ProtocolHdr.byIsReply = FALSE;          //是否获取投递成功还是失败的协议包
    st_ProtocolHdr.wTail = NETENGIEN_COMMUNICATION_PACKET_PROTOCOL_TAIL;

    NETENGINE_PROTOCOL_XMQ st_ProtoXMQ;
    memset(&st_ProtoXMQ, '\0', sizeof(NETENGINE_PROTOCOL_XMQ));
    //保存时间，单位秒，如果为0，获取一次后被抛弃。-1 永久存在，PacketKey不能为空，如果有多个永久存在的包wPacketSerial 最好设置值,不然你无法获取多个包
    st_ProtoXMQ.nKeepTime = 0;
    //哪种类型用户可以获取0 不关心 1 TCP 2 UDP
    st_ProtoXMQ.nIPType = 0;
    //如果不需要包的唯一识别码，可以不要，全部置为0即可

    st_ProtocolHdr.unPacketSize = sizeof(NETENGINE_PROTOCOL_XMQ) + _tcslen(lpszMsgBuffer);

    *pInt_Len = sizeof(NETENGINE_PROTOCOLHDR) + st_ProtocolHdr.unPacketSize;
    memcpy(ptszMsgBuffer, &st_ProtocolHdr, sizeof(NETENGINE_PROTOCOLHDR));
    memcpy(ptszMsgBuffer + sizeof(NETENGINE_PROTOCOLHDR), &st_ProtoXMQ, sizeof(NETENGINE_PROTOCOL_XMQ));
    memcpy(ptszMsgBuffer + sizeof(NETENGINE_PROTOCOLHDR) + sizeof(NETENGINE_PROTOCOL_XMQ), lpszMsgBuffer, _tcslen(lpszMsgBuffer));
}
void MQ_Get(TCHAR *ptszMsgBuffer, int *pInt_Len)
{
    LPCTSTR lpszAddr = _T("XYRYMQ_CODE");
    NETENGINE_PROTOCOLHDR st_ProtocolHdr;
    NETENGINE_PROTOCOL_XMQ st_ProtoXMQ;

    memset(&st_ProtocolHdr, '\0', sizeof(NETENGINE_PROTOCOLHDR));
    memset(&st_ProtoXMQ, '\0', sizeof(NETENGINE_PROTOCOL_XMQ));

    st_ProtocolHdr.wHeader = NETENGIEN_COMMUNICATION_PACKET_PROTOCOL_HEADER;
    st_ProtocolHdr.unOperatorType = ENUM_ENGINE_COMMUNICATION_PROTOCOL_TYPE_XMQ;
    st_ProtocolHdr.unOperatorCode = NETENGINE_COMMUNICATION_PROTOCOL_OPERATOR_CODE_MQ_REQGET;
    st_ProtocolHdr.byVersion = 1;
    st_ProtocolHdr.wPacketSerial = 0;
    st_ProtocolHdr.byIsReply = TRUE;                  //必须为真
    st_ProtocolHdr.wTail = NETENGIEN_COMMUNICATION_PACKET_PROTOCOL_TAIL;

    st_ProtocolHdr.unPacketSize = sizeof(NETENGINE_PROTOCOL_XMQ);

    *pInt_Len = sizeof(NETENGINE_PROTOCOLHDR) + st_ProtocolHdr.unPacketSize;
    memcpy(ptszMsgBuffer, &st_ProtocolHdr, sizeof(NETENGINE_PROTOCOLHDR));
    memcpy(ptszMsgBuffer + sizeof(NETENGINE_PROTOCOLHDR), &st_ProtoXMQ, sizeof(NETENGINE_PROTOCOL_XMQ));
}
//离开包
void MQ_Leave(TCHAR *ptszMsgBuffer, int *pInt_Len)
{
    NETENGINE_PROTOCOLHDR st_ProtocolHdr;
    NETENGINE_PROTOCOL_XMQ st_ProtoXMQ;

    memset(&st_ProtocolHdr, '\0', sizeof(NETENGINE_PROTOCOLHDR));
    memset(&st_ProtoXMQ, '\0', sizeof(NETENGINE_PROTOCOL_XMQ));

    st_ProtocolHdr.wHeader = NETENGIEN_COMMUNICATION_PACKET_PROTOCOL_HEADER;
    st_ProtocolHdr.unOperatorType = ENUM_ENGINE_COMMUNICATION_PROTOCOL_TYPE_XMQ;
    st_ProtocolHdr.unOperatorCode = NETENGINE_COMMUNICATION_PROTOCOL_OPERATOR_CODE_MQ_REQDEL;
    st_ProtocolHdr.byVersion = 1;
    st_ProtocolHdr.wPacketSerial = 0;
    st_ProtocolHdr.unPacketSize = 1;
    st_ProtocolHdr.byIsReply = FALSE;
    st_ProtocolHdr.wTail = NETENGIEN_COMMUNICATION_PACKET_PROTOCOL_TAIL;

    st_ProtoXMQ.nIPType = 2;
    _tcscpy_s(st_ProtoXMQ.tszMQKey, MAX_PATH,"XYRYMQ_CODE");

    st_ProtocolHdr.unPacketSize = sizeof(NETENGINE_PROTOCOL_XMQ);

    *pInt_Len = sizeof(NETENGINE_PROTOCOLHDR) + st_ProtocolHdr.unPacketSize;
    memcpy(ptszMsgBuffer, &st_ProtocolHdr, sizeof(NETENGINE_PROTOCOLHDR));
    memcpy(ptszMsgBuffer + sizeof(NETENGINE_PROTOCOLHDR), &st_ProtoXMQ, sizeof(NETENGINE_PROTOCOL_XMQ));
}
//UDP
int main(int argc, char **argv)
{
    BOOL bSend = FALSE;
    LPCTSTR lpszServiceAddr = _T("192.168.1.6");
    LPCTSTR lpszMsgBuffer = _T("123456789aaa");
    int nLen = 0;

    TCHAR tszClientAddr[64];
    TCHAR tszMsgBuffer[2048];

    memset(tszClientAddr, '\0', sizeof(tszClientAddr));
    memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));
    SOCKET hSocket;
    if (!NetClient_UDPSelect_Create(&hSocket,lpszServiceAddr, 5656))
    {
        printf("连接失败！\n");
        return -1;
    }
    printf("连接成功！\n");
    if (bSend)
    {
        MQ_Post(lpszMsgBuffer, tszMsgBuffer, &nLen);
        if (!NetClient_UDPSelect_SendMsg(hSocket,tszMsgBuffer, nLen))
        {
            printf("发送投递失败！\n");
            return -1;
        }
        Sleep(1000);
        printf("投递数据包成功！\n");
    }
    else
    {
        if (!NetClient_UDPSelect_Bind(hSocket, 56561))
        {
            return FALSE;
        }
        MQ_Get(tszMsgBuffer, &nLen);
        if (!NetClient_UDPSelect_SendMsg(hSocket, tszMsgBuffer, nLen))
        {
            printf("发送获取失败！\n");
            return -1;
        }

        while (TRUE)
        {
            nLen = 2048;
            memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));
            if (NetClient_UDPSelect_RecvMsg(hSocket, tszMsgBuffer, &nLen, tszClientAddr))
            {
                NETENGINE_PROTOCOLHDR st_ProtocolHdr;
                memset(&st_ProtocolHdr, '\0', sizeof(NETENGINE_PROTOCOLHDR));

                memcpy(&st_ProtocolHdr, tszMsgBuffer, sizeof(NETENGINE_PROTOCOLHDR));
                printf("接受到数据：%d 类型：%X，长度：%d，内容：%s\n", nLen, st_ProtocolHdr.unOperatorCode, st_ProtocolHdr.unPacketSize, tszMsgBuffer + sizeof(NETENGINE_PROTOCOLHDR));
                break;
            }
            Sleep(1);
        }
    }

    nLen = 2048;
    memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));
    //MQ_Leave(tszMsgBuffer, &nLen);
    //NetClient_UDPSelect_SendMsg(hSocket, tszMsgBuffer, nLen);
    return 0;
}
//TCP
int main1(int argc, char **argv)
{
    BOOL bSend = FALSE;
    LPCTSTR lpszServiceAddr = _T("192.168.1.6");
    LPCTSTR lpszMsgBuffer = _T("123456789aaa");
    int nLen = 0;
    TCHAR tszMsgBuffer[2048];
    memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));

    if (!NetClient_TCPSelect_Create(lpszServiceAddr, 5655, &m_Socket))
    {
        printf("连接失败！\n");
        return -1;
    }
    printf("连接成功！\n");

    if (bSend)
    {
        MQ_Post(lpszMsgBuffer, tszMsgBuffer, &nLen);
        if (!NetClient_TCPSelect_SendMsg(m_Socket, tszMsgBuffer, nLen))
        {
            printf("发送投递失败！\n");
            return -1;
        }
        Sleep(1000);
        printf("投递数据包成功！\n");
        /*
        while (TRUE)
        {
        nLen = 2048;
        memset(tszMsgBuffer,'\0',sizeof(tszMsgBuffer));
        if (NetClient_TCPSelect_RecvMsg(m_Socket,tszMsgBuffer,&nLen))
        {
        NETENGINE_PROTOCOLHDR st_ProtocolHdr;
        memset(&st_ProtocolHdr,'\0',sizeof(NETENGINE_PROTOCOLHDR));

        memcpy(&st_ProtocolHdr,tszMsgBuffer,sizeof(NETENGINE_PROTOCOLHDR));
        printf("接受到数据：类型：%X\n",st_ProtocolHdr.unOperatorCode);
        break;
        }
        sleep(1);
        }*/
    }
    else
    {
        MQ_Get(tszMsgBuffer, &nLen);
        if (!NetClient_TCPSelect_SendMsg(m_Socket, tszMsgBuffer, nLen))
        {
            printf("发送获取失败！\n");
            return -1;
        }

        while (TRUE)
        {
            memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));
            if (NetClient_TCPSelect_RecvMsg(m_Socket, tszMsgBuffer, &nLen))
            {
                NETENGINE_PROTOCOLHDR st_ProtocolHdr;
                memset(&st_ProtocolHdr, '\0', sizeof(NETENGINE_PROTOCOLHDR));

                memcpy(&st_ProtocolHdr, tszMsgBuffer, sizeof(NETENGINE_PROTOCOLHDR));
                printf("接受到数据：类型：%X，长度：%d，内容：%s\n", st_ProtocolHdr.unOperatorCode, st_ProtocolHdr.unPacketSize, tszMsgBuffer + sizeof(NETENGINE_PROTOCOLHDR));
                break;
            }
            Sleep(1);
        }
    }
    NetClient_TCPSelect_Close(m_Socket);
    return 0;
}

